import { createRouter, createWebHistory } from 'vue-router'
import Cookie from "js-cookie";
const Index = () => import('../views/Index.vue')
const Login = () => import('../views/Login.vue')
const _404View = () => import('../views/404.vue')
const Flush = () => import('../views/Flush.vue')


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/index/homepage'
    },
    {
      name: 'index',
      path: '/index',
      component: Index,
      children: [{
        name: 'flush',
        path: 'flush/:path',
        component: Flush,
        meta:{title:''}
      },{
        name: 'info',
        path: 'info',
        component: () => import('../views/admin/Info.vue'),
        meta:{title:'admin.info'}
      },{
        name: 'changepassword',
        path: 'changepassword',
        component: () => import('../views/admin/ChangePassword.vue'),
        meta:{title:'admin.password'}
      }]
    },
    {
      name: 'login',
      path: '/login',
      component: Login
    },
    {
      name: '404',
      path: '/404',
      component: _404View
    },
    {
      //匹配不存在的任何路由
      path: "/:path(.*)+",
      redirect: "/404",
    }
  ]
})

router.beforeEach((to,from)=>{
  const token = Cookie.get("token");  
  if(!token&&to.name!=='login'){
    router.replace({path:'/login',query:{back:to.fullPath}})
    return false
  }
  return true

})

export default router